package org.cloud.admin.auth.service.impl;

import java.util.Arrays;
import java.util.List;

import org.cloud.admin.auth.holder.AppHolder;
import org.cloud.admin.auth.service.SysRoleMenuService;
import org.cloud.module.common.dao.SysRoleMenuDao;
import org.cloud.module.common.entity.SysRoleMenuEntity;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



/**
 * 角色与菜单对应关系
 * @author wangyuan
 * @since 2019年6月26日 下午11:16:41
 *
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

	@Override
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		deleteBatch(new Long[]{roleId});

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		for(Long menuId : menuIdList){
			SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
			sysRoleMenuEntity.setMenuId(menuId);
			sysRoleMenuEntity.setRoleId(roleId);
			sysRoleMenuEntity.setCreatedUser(String.valueOf(AppHolder.getLoginedUserId()));
			sysRoleMenuEntity.setUpdatedUser(String.valueOf(AppHolder.getLoginedUserId()));

			this.save(sysRoleMenuEntity);
		}
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return baseMapper.queryMenuIdList(roleId);
	}

	@Override
	public boolean deleteBatch(Long[] roleIds){
		QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.in("role_id", Arrays.asList(roleIds));
		
		return remove(queryWrapper);
	}

}
